package com.huqing.icu.rentchatsession.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Description AI对话接口
 * @Author huqing
 * @Date 2025/5/31 17:43
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/chat")
@Tag(name = "AI对话接口")
public class ChatController {

    private final ChatClient chatClient;

    private final ChatMemory chatMemory;

    //如果不写produces = "text/html;charset=utf-8"，大模型输出会乱码
    @GetMapping(value = "/v1", produces = "text/html;charset=utf-8")
    public Flux<String> chatV1(@RequestParam("prompt") String prompt) {
        //String prompt = "你是谁";
        String chatId = "1";
        //前端传给我的会话ID，需要传给advisors，让advisors去管理，也就是添加会话ID到AdvisorsContext上下文中,根据不同的会话ID做用户会话的区分
        return chatClient.prompt().user(prompt).advisors(o -> o.param(ChatMemory.CONVERSATION_ID, chatId)).stream().content();
    }
}
